const Router = require('express')
const swaggerJsdoc = require('swagger-jsdoc');
const router = new Router()
const message = require('./controllers/message')
const auth = require('./controllers/auth')
const report = require('./controllers/report')
const jwt = require('jsonwebtoken');

function authenticateToken(req, res, next) {
    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];
    if (token == null) return res.sendStatus(401);

    jwt.verify(token, 'secret_key', (err, user) => {
        if (err) return res.sendStatus(403);
        req.user = user;
        next();
    });
}

const { Pool } = require('pg');
const pool = new Pool({
    user: 'postgres',
      host: 'localhost',
      database: 'postgres',
      password: '1234',
      port: 5432,
  });

  router.post('/register', auth.registration)
router.post('/login', auth.login)
router.post('/refresh-token', auth.refreshToken)
router.get('/users', authenticateToken, auth.users)
const multer = require('multer');
const fs = require('fs');

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/');
    },
    filename: function (req, file, cb) {
        cb(null, `Report ${file.originalname}`);
    }
});
const upload = multer({ storage: storage });

//Population endpoints
router.get('/reports', authenticateToken, report.reportGet)
router.post('/report', authenticateToken, upload.single('file'), report.reportPost)
router.put('/report', authenticateToken, upload.single('file'), report.reportPut)

//Population_growth endpoints
router.get('/messages', authenticateToken, message.messageGet)
router.post('/message', authenticateToken, message.messagePost)


module.exports = router